package com.zhiyi.coco.controller;

import com.zhiyi.coco.entity.SysUser;
import com.zhiyi.coco.service.SysUserService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/login")
@Log4j
public class LoginController {
    @Autowired
    private SysUserService service;

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping(value = "/success/{logincode}/{pwd}")
    public Object login(@PathVariable String logincode, @PathVariable String pwd, HttpSession session) {
        SysUser loginBycode=service.loginByCode(logincode,pwd);
        SysUser login = service.login(loginBycode);
        if (login != null) {
            //如果存在，保存用户数据，跳转回开发者界面
            session.setAttribute("user", login);
            return login;
        }
        return null;
    }
    /**
     * 退出
     *
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "login";
    }
}
